layui.use(["element", "layer", "table", "form"], function() {
	var $ = layui.jquery;
	var layer = layui.layer;
	var table = layui.table;
	var sessionStorage = window.sessionStorage;
	var userId = sessionStorage.getItem("userId");
	var userName = sessionStorage.getItem("userName");
	var ip = "http://localhost"; //后台ip
	var port = ":9014"; //端口

	function convertObj(data) {
		var _result = [];
		for(var key in data) {
			var value = data[key];
			if(value.constructor == Array) {
				value.forEach(function(_value) {
					_result.push(key + "=" + _value);
				});
			} else {
				_result.push(key + '=' + value);
			}
		}
		return _result.join('&');
	}

	$(".layui-btn").click(function() {
		var collection = $(".layui-btn");
		$.each(collection, function() {
			$(this).addClass("layui-btn-primary");
			$(this).removeClass("layui-btn-normal");
		})
		$(this).removeClass("layui-btn-primary");
		$(this).addClass("layui-btn-normal");
		var name = $(this).attr("id");
		var param = {};
		param["belongProduct"] = $("select").val();
		switch(name) {
			case "fileType1":
				param["fileType"] = 1;
				break;
			case "fileType3":
				param["fileType"] = 3;
				break;
			default:
				param[name] = userId;
		}
		table.render({
			method: "get",
			url: ip + port + '/file/searchByPage',
			elem: '#table',
			height: 450,
			where: param,
			page: true,
			skin: 'nob',
			cols: [
				[{
						field: "id",
						title: "ID",
						width: 30
					},
					{
						field: "belongProductName",
						title: "所属产品",
						width: 130
					},
					{
						field: "belongProjectName",
						title: "所属项目",
						width: 180
					},
					{
						field: "name",
						title: "需求名称",
						width: 400,
						style: "cursor:pointer;color:blue",
						event: 'download'
					},

					{
						field: "belongTaskName",
						title: "所属任务",
						width: 180
					},
					{
						field: "belongBugName",
						title: "所属Bug",
						width: 180
					},
					{
						field: "belongDemandName",
						title: "所属需求",
						width: 180
					}
				]
			],
			parseData: function(res) {
				return {
					"code": 0,
					"count": res.records,
					"data": res.data
				}
			}
		});
		table.on('tool(table)', function(obj) {
			var data = obj.data;
			if(obj.event === 'download') {
				layer.confirm('是否下载该附件？', {
						btn: ['确定'] //按钮
					}, function() {
						window.open(data.filePosition);
					});
			}
		});
	})
})